$.extend(jQuery.validator.messages, {
    required: "必选字段",
    remote: "请修正该字段",
    email: "请输入正确格式的电子邮件",
    url: "请输入合法的网址",
    date: "请输入合法的日期",
    dateISO: "请输入合法的日期 (ISO).",
    number: "请输入合法的数字",
    digits: "只能输入整数",
    creditcard: "请输入合法的信用卡号",
    equalTo: "请再次输入相同的值",
    accept: "请输入拥有合法后缀名的字符串",
    maxlength: jQuery.validator.format("请输入一个 长度最多是 {0} 的字符串"),
    minlength: jQuery.validator.format("请输入一个 长度最少是 {0} 的字符串"),
    rangelength: jQuery.validator.format("请输入 一个长度介于 {0} 和 {1} 之间的字符串"),
    range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
    max: jQuery.validator.format("请输入一个最大为{0} 的值"),
    min: jQuery.validator.format("请输入一个最小为{0} 的值")
});
$.validator.addMethod("isPhone", function(value, element) {
    var length = value.length;
    var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "请填写正确的手机号码");
$.validator.setDefaults({
    debug: true
})
var formvalidate = false;
$(function () {
    $("#loginform").validate({
        debug: true,
        rules:{
            account:{
                required: true,
                isPhone:true
            },
            valcode:{
                required: true,
                maxlength:4
            },
            phonevalcode:{
                required: true,
                maxlength:6
            }
        },
        messages: {
            account:{
                required: '请输入手机号',
                isPhone:'请输入正确的手机号'
            },
            valcode:{
                required: '请输入图形验证码',
                maxlength:'请输入正确图形验证码'
            },
            phonevalcode:{
                required: '请输入手机验证码',
                maxlength:'请输入正确手机验证码'
            }
        },
        success: function(label) {
            formvalidate=true;
        }
    });
    $('.sendcode').on('click',function(){ 
        var phone = Utils.paseData($('#account').val());
        var valcode = Utils.paseData($('#valcode').val());
        var boolsub = true;
        if(phone == ''){            
            if($('#account-error').length>0){
                $('#account-error').html('请输入手机号码');
            }else{
                $('#account').after('<label id="account-error" class="error" for="account">请输入手机号</label>');
            }
            boolsub = false;
        }
        if(valcode == ''){
            if($('#valcode-error').length>0){
                $('#valcode-error').html('请输入手机号码');
            }else{
                $('#valcode').after('<label id="valcode-error" class="error" for="valcode">请输入图形验证码</label>');
            }
            boolsub = false;
        }
        if(boolsub==true && formvalidate==true){
            var count = 120;
            $.getJSON("https://id.renbenzhihui.com/index.php/sendTool/sendmsg?t=3&auth="+valcode+"&rt=?&mobile="+phone+"&jsoncallback=?",function(data){
                if(data.code/1 == 0){
                    const countDown = setInterval(() => {
                        if (count === 0) {
                            $('.sendcode').val('重新发送').removeAttr('disabled');
                            $('.sendcode').css({
                                background: '#ff9400',
                                color: '#fff',
                            });
                            clearInterval(countDown);
                        }else{
                            $('.sendcode').attr('disabled', true);
                            $('.sendcode').css({
                                background: '#d8d8d8',
                                color: '#707070',
                            });
                            $('.sendcode').val(count + '秒后可重新获取');
                        }
                        count--;
                    },1000);
                }else{
                    new $.zui.Messager(data.message, {                
                        type:'danger',
                        placement:'center' // 定义消息图标
                    }).show();
                }
            });
        }
    });
    $("#valcode").focus(function(){
        var phone = Utils.paseData($('#account').val());
        var boolsub = true;
        if(phone == ''){            
            if($('#account-error').length>0){
                $('#account-error').html('请输入手机号码');
            }else{
                $('#account').after('<label id="account-error" class="error" for="account">请输入手机号</label>');
            }
            boolsub = false;
        } else if($('#account-error').html().length>0){
            boolsub = false;
        }
        if(boolsub){
            $('#codebox').html('<img id="imgcode" src="'+ 'https://id.renbenzhihui.com/index.php/pub/authcode?u='+phone +'"  onclick="" width="200">');
            // $('#imgcode').attr('src','https://id.renbenzhihui.com/index.php/pub/authcode?u='+phone);
            $('#imgcode').attr('onclick','this.src=\'https://id.renbenzhihui.com/index.php/pub/authcode?u='+phone+'&\'+new Date().getTime();');
        }
        
    });
    $('#submit').click(function(){
        if(formvalidate){
            var phone = Utils.paseData($('#account').val());
            var phonevalcode = Utils.paseData($('#phonevalcode').val());
            $.post('/Login/LoginAc', {phone:phone,phonevalcode:phonevalcode}, function(result) {
               result = JSON.parse(result);
               if(result.code/1 == 1){
                    $.zui.store.set('kp_token', result.data.token);   
                    location.href='/Main/Index';
               }else{
                new $.zui.Messager(result.msg, {                
                    type:'danger',
                    placement:'center' // 定义消息图标
                }).show();
               }
           });
        }
    })
});